// pages/setfood/setfood.js
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    ftype:['请选择'],
    ftlist:[],
    ftindex:0,
    newtype:'',
    istrue:false,

    files: [], 
    imgsrc: '',
    foodlogo: '../../../img/me.jpg',
    food: { },
    foodPrice:{}



  },

  onLoad: function (options){
    var id = options.id;
   // console.log("setfood-id:"+id);
    var that = this; 
    
    if (id && id !=0){
      this.getTypeList(function(){
        that.getFoodDetail(id);
      });
    }else{
      this.getTypeList();
    }
    
  },
  getFoodDetail:function(id){
    var that=this;
    util.fetchGet(
      util.url.shop_food_detail ,
      // 'food/detail' + "?id=" + id,
      function (ret) {
        var food = ret.data;
        /*var foodtypes = food.foodTypes;
        for (var i = 0; i < foodtypes.length; i++) {
          that.data.ftlist.push(foodtypes[i]);
          that.data.ftype.push(foodtypes[i].name);
        }*/
        for (var i = 0; i < that.data.ftype.length; i++) {
          if (food.foodType.name == that.data.ftype[i]) {
            that.setData({
              ftindex: i
            })
          }
        }
        that.setData({
          food: food,
          foodlogo: wx.getStorageSync('host') + '/file/image/' + food.pic,
         // ftype: that.data.ftype,
         // ftlist: that.data.ftlist

        })
      }
    )
  },
  openDialog: function () {
    this.setData({
      istrue: true
    })
  },
  closeDialog: function () {
    this.setData({
      istrue: false
    })
  },
  setfttype:function(e){
    this.setData({
      newtype:e.detail.value
    })
  },
  addFoodType:function(){
    var that=this;
    var data={
      name: this.data.newtype,
      snum:0,
      state:0
    }
    util.fetchPost(
     // '/food/typeadd',
      util.url.shop_food_typeadd,
      data,
      function(ret){
        that.data.ftlist.push(ret.data);
        that.data.ftype.push(data.name);
        that.setData({
          istrue: false,
          newtype: '', 
          ftype: that.data.ftype
        })
      }
    )
    
  },
  getTypeList:function(callback){
    var that=this;
    util.fetchGet(
      util.url.shop_food_typelist  ,
      function(ret){
        for(var i=0;i<ret.data.length;i++){
          that.data.ftlist.push(ret.data[i]);
          that.data.ftype.push(ret.data[i].name);
        }
        that.setData({          
          ftype: that.data.ftype,
          ftlist: that.data.ftlist
        })
        if (callback){
          callback();
        }
      }
    )
  },

  bindftypeChange:function(e){
    
    this.setData({
      ftindex: e.detail.value
    })
  }, 
  
  bindInput1: function (e) {
    this.setData({
      'food.name': e.detail.value
    })
  },
  bindInput2: function (e) {
    this.setData({
      'food.oprice': e.detail.value 
    })
  },
  bindInput3: function (e) {
    this.setData({
      'food.price': e.detail.value  
    })
  },
  bindInput4: function (e) {
    this.setData({
      'food.idt': e.detail.value
    })
  },
  nicknameinput:function(e){
    this.setData({
      'food.nickname': e.detail.value
    })
  },
  bindInput5: function (e) {
    this.setData({
      'food.taste': e.detail.value
    })
  },
  bindsubmit: function () {
    var that = this;
    let food = that.data.food;
    var foodtype = that.data.ftlist[that.data.ftindex-1];
    food.foodType = { id: foodtype.id, name: foodtype.name};
    
    util.fetchPost(
      //'food/add?',
      util.url.shop_food_add,
      food,
      function (ret) {
        console.log(ret);
        wx.navigateBack();
      }
    )
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: wx.getStorageSync('host') + '/file/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'shop': 'test'
          },
          success(res) {
            const data = JSON.parse(res.data);
            //do something
            that.setData({
              'food.pic': data.data,
              'foodlogo': wx.getStorageSync('host') + '/file/image/' + data.data
            })
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  }
})